let repair = require('../../../api/ca_repair.js');
let that = null;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        repair: {},// 用于接受传来的数据
        user: {},// 缓存里的用户信息
        hiddenmodalput: true,//自定义弹窗初始化
        title: '',//设置输入框标题
        content: '',// 获取多行文本的值
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        that = this;
        let repair = JSON.parse(options.repair);
        let user = wx.getStorageSync('user');
        that.setData({
            repair: repair,
            user: user,
        })
    },

    /**
     * 图片查看按钮
     */
    ViewImage(e) {
        wx.previewImage({
            urls: this.data.repair.imgList,
            current: e.currentTarget.dataset.url
        });
    },

    /** 
     * 信息复制 
     */ 
    copy: function(){ 
        wx.setClipboardData({ 
          data: that.data.repair.order_time+'你有新的订单！姓名:'+that.data.repair.realName+'，宿舍：'+that.data.repair.dormNum+'，手机号码：'+that.data.repair.phone+'，问题：'+that.data.repair.reason,
          success: function (res) { 
            wx.getClipboardData({ 
              success: function (res) { 
                wx.showToast({ 
                  title: '获取订单信息成功！' 
                }) 
              } 
            })
            console.log(res) 
          } 
        }) 
    }, 

    /**
     * 维修人员点击受理按钮
     */
    async repairAccept() {
        console.log('update state to 0');
        await wx.showLoading({
            title: '受理中',
            mask: true,
        })
        let data = {
            repair_openid: that.data.user._openid,
            state: 0,
            _updateTime: Date.now(),
        }
        // 1、修改数据库
        const result = await repair.updateRepair_tabel(that.data.repair._id, data, that).then(res => { return res; });
        wx.hideLoading({
            success: (res) => { },
        })
        // 判断是否更新成功
        if (result.result.errMsg == "document.update:ok") {
            console.log("update success!")
            // 2、修改当前页数据
            await that.setData({
                ['repair.state']: 0,
                ['repair.repair_openid']: that.data.user._openid,
            })
            // 3、修改上一页数据
            let page = getCurrentPages();
            let prevPage = page[page.length - 2];// 上一页
            let repair_tabel = prevPage.data.repair_tabel;
            // 去除所有列进行判断
            for (let i = 0; i < repair_tabel.length; i++) {
                // 判断是否为修改列
                if (repair_tabel[i]._id == that.data.repair._id) {
                    prevPage.setData({
                        ['repair_tabel[' + i + '].repair_openid']: that.data.user._openid,
                        ['repair_tabel[' + i + '].state']: 0
                    })
                    break;
                }
            }
        } else {
            console.log("update fail!")
            wx.showToast({
                title: '受理失败',
                icon: 'none',
                duration: 2000,
            })
        }
    },

    /**
     * 维修人员点击完成按钮
     */
    async repairSuccess() {
        console.log('repair update state to 1');
        that.setData({
            hiddenmodalput: false,
            title: '备注'
        })
    },

    /**
     * 报修人员点击完成按钮
     */
    async userSuccess() {
        console.log('user update state to 1');
        that.setData({
            hiddenmodalput: false,
            title: '评论'
        })
    },

    /**
     * 维修人员点击备注按钮
     */
    async repairRemarks() {
        console.log('repair update remarks');
        that.setData({
            hiddenmodalput: false,
            title: '备注'
        })
    },

    /**
     * 保修人员点击评论按钮
     */
    async userComment() {
        console.log('user update command');
        that.setData({
            hiddenmodalput: false,
            title: '评论'
        })
    },

    /**
     * 点击{{title}}提交按钮事件
     */
    async modalconfirm() {
        let content = that.data.content;
        let data = {};
        // 判断是否有输入内容
        if (content != '' && content != undefined) {
            // 判断是报修人员点击还是维修人员点击
            if (that.data.repair.repair_openid == that.data.user._openid && that.data.repair.repair_remarks == undefined) {
                console.log('repair update state to 1 ongoing');
                await wx.showLoading({
                    title: '备注中',
                    mask: true,
                })
                data = {
                    state: 1,
                    repair_remarks: content,
                    _updateTime: Date.now(),
                }
                // 1、修改数据库
                const result = await repair.updateRepair_tabel(that.data.repair._id, data, that).then(res => { return res; });
                wx.hideLoading({
                    success: (res) => { },
                })
                // 判断是否更新成功
                if (result.result.errMsg == "document.update:ok") {
                    console.log("update success!")
                    // 2、修改当前页数据
                    await that.setData({
                        ['repair.state']: 1,
                        ['repair.repair_remarks']: content,
                        hiddenmodalput: true,
                        title: '',
                    })
                    // 3、修改前一页数据
                    let page = getCurrentPages();
                    let prevPage = page[page.length - 2];// 上一页
                    let repair_tabel = prevPage.data.repair_tabel;
                    // 去除所有列进行判断
                    for (let i = 0; i < repair_tabel.length; i++) {
                        // 判断是否为修改列
                        if (repair_tabel[i]._id == that.data.repair._id) {
                            prevPage.setData({
                                ['repair_tabel[' + i + '].repair_remarks']: content,
                                ['repair_tabel[' + i + '].state']: 1
                            })
                            break;
                        }
                    }
                } else {
                    console.log("update fail!")
                    wx.showToast({
                        title: '备注失败',
                        icon: 'none',
                        duration: 2000,
                    })
                }
            } else if (that.data.repair._openid == that.data.user._openid && that.data.repair.user_comment == undefined) {
                console.log('repair update state to 1 ongoing');
                await wx.showLoading({
                    title: '评论中',
                    mask: true,
                })
                data = {
                    state: 1,
                    user_comment: content,
                    _updateTime: Date.now(),
                }
                // 1、修改数据库
                const result = await repair.updateRepair_tabel(that.data.repair._id, data, that).then(res => { return res; });
                wx.hideLoading({
                    success: (res) => { },
                })
                // 判断是否更新成功
                if (result.result.errMsg == "document.update:ok") {
                    console.log("update success!")
                    // 2、修改当前页数据
                    await that.setData({
                        ['repair.state']: 1,
                        ['repair.user_comment']: content,
                        hiddenmodalput: true,
                        title: '',
                    })
                    // 3、修改前一页数据
                    let page = getCurrentPages();
                    let prevPage = page[page.length - 2];// 上一页
                    let repair_tabel = prevPage.data.repair_tabel;
                    // 去除所有列进行判断
                    for (let i = 0; i < repair_tabel.length; i++) {
                        // 判断是否为修改列
                        if (repair_tabel[i]._id == that.data.repair._id) {
                            prevPage.setData({
                                ['repair_tabel[' + i + '].user_comment']: content,
                                ['repair_tabel[' + i + '].state']: 1
                            })
                            break;
                        }
                    }
                } else {
                    console.log("update fail!")
                    wx.showToast({
                        title: '评论失败',
                        icon: 'none',
                        duration: 2000,
                    })
                }
            } else {
                console.log('confirm error');
                wx.showToast({
                    title: '出错，请重试',
                    icon: 'none',
                    duration: 2000,
                })
            }
        } else {
            wx.showToast({
                title: '请输入' + that.data.title,
                icon: 'none',
                duration: 2000,
            })
        }
    },

    /**
     * 点击{{title}}取消按钮事件
     */
    async modalcancel() {
        // 判断维修表当前状态时进行中还是完成
        if (that.data.repair.state == 0) {
            console.log('repair update state to 1 ongoing');
                await wx.showLoading({
                    title: '修改状态中',
                    mask: true,
                })
                let data = {
                    state: 1,
                    _updateTime: Date.now(),
                }
                // 1、修改数据库
                const result = await repair.updateRepair_tabel(that.data.repair._id, data, that).then(res => { return res; });
                wx.hideLoading({
                    success: (res) => { },
                })
                // 判断是否更新成功
                if (result.result.errMsg == "document.update:ok") {
                    console.log("update success!")
                    // 2、修改当前页数据
                    await that.setData({
                        ['repair.state']: 1,
                        hiddenmodalput: true,
                        title: '',
                    })
                    // 3、修改前一页数据
                    let page = getCurrentPages();
                    let prevPage = page[page.length - 2];// 上一页
                    let repair_tabel = prevPage.data.repair_tabel;
                    // 去除所有列进行判断
                    for (let i = 0; i < repair_tabel.length; i++) {
                        // 判断是否为修改列
                        if (repair_tabel[i]._id == that.data.repair._id) {
                            prevPage.setData({
                                ['repair_tabel[' + i + '].state']: 1
                            })
                            break;
                        }
                    }
                } else {
                    console.log("update fail!")
                    wx.showToast({
                        title: '备注失败',
                        icon: 'none',
                        duration: 2000,
                    })
                }
        } else if (that.data.repair.state == 1) {
            console.log('cancel' + that.data.title);
            that.setData({
                hiddenmodalput: true,
                title: '',
            })
        } else {
            console.log('cancel error');
            wx.showToast({
                title: '出错，请重试',
                icon: 'none',
                duration: 2000,
            })
        }
    },

    /**
     * 右上角分享
     */
    onShareAppMessage() {
        return {
            title: '首页',
            path: '/pages/CA/home/home',
            imageUrl: '../../../images/share.png'
        }
    },
})